package com.cn.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: client
 * @description: 驼峰命名与数据下换线互转
 * @author: yihanyuan
 * @create: 2019-03-25 10:37
 **/
public class CamelUtil {
    private static Pattern linePattern = Pattern.compile("_(\\w)");

    /**
     * @return
     * @description 下划线转驼峰
     * @author YiHanYuan
     * @params
     * @date 2019/3/25 11:24
     **/
    public static String lineToCamel(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    /**
     * @return
     * @description 驼峰转下划线,
     * @author YiHanYuan
     * @params
     * @date 2019/3/25 11:24
     **/
    public static String camelToLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
